import 'server-only' //代表仅服务端可用
import Joi from 'joi';
import resFn from '@/app/api/util/res'
import { NextRequest } from 'next/server';
import { logError, logSpecial } from '@/app/api/util/io/log';
import { getBody, judge, permissionJudge } from '@/app/api/util/res/handle';
import { IntegrityCheckReq, IntegrityCheckRes } from '@/types/api/file/sliceUpload';
import { numberJoi, stringJoi } from '@/app/api/util/Joi';
import { getDir } from '@/app/api/util/io';
import { getTempDir } from '../../getPath';
export const dynamic = 'force-dynamic';

/**分片上传后，完整性校验 接口*/
export async function POST(request: NextRequest) {
    try {
        const user_id = "JZ8740"
        const { count, md5 } = await judge(await getBody<IntegrityCheckReq>(request), { count: numberJoi(), md5: stringJoi() })
        const files = await getDir(getTempDir(user_id, md5)) //获得文件列表
        const judgeSet = new Set(Array.from({ length: count }, (k, i) => i))
        files.forEach((file) => {
            judgeSet.delete(parseInt(file.name))
        })
        return resFn.success<IntegrityCheckRes>({
            missingArr: [...judgeSet]
        });
    } catch (error: any) {
        logError('分片上传后，完整性校验 接口', error)
        return resFn.errorWithReject(error);
    }
}